﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Core;

namespace TestProject
{
    public partial class NewChromosomeTable : Form
    {
        MainWindow mw;
        public NewChromosomeTable(MainWindow mw)
        {
            InitializeComponent();
            this.mw=mw;
        }
        private void create_Click(object sender, EventArgs e)
        {
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + mw.model.model_name + "\\Tables\\ChromosomeTables\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("chromosome_table");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("ChromosomePairsAmount", pairs_box.Text);
            writer.WriteStartElement("chromosomes");
            for (int i = 0; i < Convert.ToInt32(pairs_box.Text); i++)
            {
                writer.WriteStartElement("chromosome");
                writer.WriteAttributeString("Name", "default");
                writer.WriteAttributeString("LocusesAmount", "0");
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(mw.model.model_path));
            XmlElement el;
            el = doc.CreateElement("table");
            el.SetAttribute("Path", "file_input\\" + mw.model.model_name + "\\Tables\\ChromosomeTables\\" + name_box.Text + ".xml");
            doc.GetElementsByTagName("tables")[0].Attributes[1].Value = (Convert.ToInt32(doc.GetElementsByTagName("tables")[0].Attributes[1].Value) + 1).ToString();
            doc.DocumentElement.SetAttribute("AmountTables", (Convert.ToInt32(doc.DocumentElement.GetAttribute("AmountTables")) + 1).ToString());
            doc.GetElementsByTagName("chromosome_tables")[0].AppendChild(el);
            doc.Save(mw.model.model_path);
            mw.treeView1.Nodes[0].Nodes[0].Nodes[1].Nodes.Add(name_box.Text);
            this.Close();
        }
        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
